package beans;

import dao.HistoryTaskDao;
import engine.BIEngine0_1.AlgorithmThread;
import engine.BIEngine0_1.ParseTaskJson;
import net.sf.json.JSONObject;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

public class RunningTaskBean implements  Serializable{

    private static final long serialVersionUID = -1374558349321944476L;
    /**
     * 用于记录暂停时候正在运行的作业id
     */
    private Map<String,List<String>> runningJobs;
    /**
     * task id
     */
    private String taskId;
    /**
     * task name
     */
    private String taskName;
    /**
     * task hash
     */
    private String taskHash;
    /**
     * used to produce runnable algorithm object
     */
    private String json;
    /**
     * Used to manage algorithmic thread objects
     */
    public ControllerObject controlThreadObject;
    /**
     * Running algorithmic thread object
     */
    public Map<String,AlgorithmThread> runningAlgorithms;
    /**
     * Algorithm objects to be run
     */
    public Map<String,Object> runnableObjects;
    /**
     * completed object hash
     */
    public ArrayList<String> completedObjectHash;
    /**
     * used to store the state of this task
     */
    private JSONObject stateJson;
    /**
     * 该任务的命令行的打印的东西
     */
    public StringBuilder state;
    /**
     * newest state
     */
    public String newestState = "";
    /**
     * stop task
     */
    public boolean isStop;

    /**
     * history task operate dao
     */
    public HistoryTaskDao historyDao;
    /**
     * format date
     */
    public SimpleDateFormat sf;
    /**
     * task is pause
     */
    public boolean isPause;
    /**
     * hashes of the firt part of runnableObjects
     */
    public Map<String,Integer> restartHashes;
    /**
     * record the newest completed algorithm
     */
    public String newestAlgorithm="";
    private String batchTaskHash;
    /**
     * record is got newest state, false:newestState needs to append new state
     *                              true:newestState need not to append
     */
    public boolean gotNewestState;
    /**
     * socket log info
     * key:<subTask hash+algorithm hash>
     * value:socketlogrecordinfo
     */
    private Map<String, SocketLogRecordInfo> socketLogRecordInfos;
    /**
     * 在子任务开始之前就已经将所涉及的算法文件都上传了，对应的ftp uri list用算法id存到这里
     */
    private Map<String,List<String>> algorithmIdAndFtpUri;
    /**
     * model上传后对应的xml文件地址
     */
    public String modelXmlDir;

    public List<String> getAlgorithmIdAndFtpUri(String key) {
        List<String> temp = algorithmIdAndFtpUri.get(key);
        List<String> result = new ArrayList<>();
        result.addAll(temp);
        return result;
    }

    public void setAlgorithmIdAndFtpUri(Map<String, List<String>> algorithmIdAndFtpUri) {
        this.algorithmIdAndFtpUri = algorithmIdAndFtpUri;
    }

    /**
     * constructor
     */
    public RunningTaskBean(String taskId, String taskHash, String taskJson, String taskName) {
        this.isStop = false;
        this.state = new StringBuilder();
        this.stateJson = new JSONObject();
        this.taskId = taskId;
        this.taskHash = taskHash;
        this.json = taskJson;
        this.taskName = taskName;
        this.completedObjectHash = new ArrayList<>();
        this.historyDao = new HistoryTaskDao();
        this.controlThreadObject = new ControllerObject();
        this.sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.runningAlgorithms = new HashMap<>();
        ParseTaskJson parser = new ParseTaskJson();
        this.runnableObjects = new HashMap<>();
    }
    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }
    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public String getTaskHash() {
        return taskHash;
    }

    public Map<String, SocketLogRecordInfo> getSocketLogRecordInfos() {
        return socketLogRecordInfos;
    }

    public void setSocketLogRecordInfos(Map<String, SocketLogRecordInfo> socketLogRecordInfos) {
        this.socketLogRecordInfos = socketLogRecordInfos;
    }

    public Map<String, List<String>> getRunningJobs() {
        return runningJobs;
    }

    public void setRunningJobs(Map<String, List<String>> runningJobs) {
        this.runningJobs = runningJobs;
    }

    public String getBatchTaskHash() {
        return batchTaskHash;
    }

    public void setBatchTaskHash(String batchTaskHash) {
        this.batchTaskHash = batchTaskHash;
    }

    public void setTaskHash(String taskHash) {
        this.taskHash = taskHash;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public Object getControlThreadObject() {
        return controlThreadObject;
    }

    public void setControlThreadObject(ControllerObject controlThreadObject) {
        this.controlThreadObject = controlThreadObject;
    }

    public Map<String,AlgorithmThread> getRunningAlgorithms() {
        return runningAlgorithms;
    }

    public void setRunningAlgorithms(Map<String,AlgorithmThread> runningAlgorithms) {
        this.runningAlgorithms = runningAlgorithms;
    }
    public JSONObject getStateJson() {
        return stateJson;
    }

    public void setStateJson(JSONObject stateJson) {
        this.stateJson = stateJson;
    }

    public Map<String, Object> getRunnableObjects() {
        return runnableObjects;
    }

    public void setRunnableObjects(Map<String, Object> runnableObjects) {
        this.runnableObjects = runnableObjects;
    }

    public String getNewestAlgorithm() {
        return newestAlgorithm;
    }

    public void setNewestAlgorithm(String newestAlgorithm) {
        this.newestAlgorithm = newestAlgorithm;
    }

    public String getModelXmlDir() {
        return modelXmlDir;
    }

    public void setModelXmlDir(String modelXmlDir) {
        this.modelXmlDir = modelXmlDir;
    }

    @Override
    public String toString() {
        return "RunningTaskBean{" +
                "taskId='" + taskId + '\'' +
                ", taskName='" + taskName + '\'' +
                ", taskHash='" + taskHash + '\'' +
                ", json='" + json + '\'' +
                ", controlThreadObject=" + controlThreadObject +
                ", runningAlgorithms=" + runningAlgorithms +
                ", runnableObjects=" + runnableObjects +
                ", completedObjectHash=" + completedObjectHash +
                ", stateJson=" + stateJson +
                ", state=" + state +
                ", newestState='" + newestState + '\'' +
                ", isStop=" + isStop +
                ", historyDao=" + historyDao +
                ", sf=" + sf +
                ", isPause=" + isPause +
                ", restartHashes=" + restartHashes +
                ", newestAlgorithm='" + newestAlgorithm + '\'' +
                ", batchTaskHash='" + batchTaskHash + '\'' +
                ", gotNewestState=" + gotNewestState +
                ", socketLogRecordInfos=" + socketLogRecordInfos +
                '}';
    }
}
